package cn.wolfcode.shop.cloud.web.controller;

import cn.wolfcode.shop.cloud.common.Result;
import cn.wolfcode.shop.cloud.domain.SeckillGood;
import cn.wolfcode.shop.cloud.redis.RedisService;
import cn.wolfcode.shop.cloud.service.ISeckillGoodService;
import cn.wolfcode.shop.cloud.util.SeckillKeyPrefixUtil;
import cn.wolfcode.shop.cloud.vo.SeckillGoodVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/seckillGood")
public class SeckillCoodController {
    @Autowired
    private ISeckillGoodService seckillGoodService;
    @Autowired
    private RedisService redisService;
    @RequestMapping("/query")
    public Result query(){
        return Result.success(seckillGoodService.queryCache());
    }

    @RequestMapping("/queryById")
    public Result queryById(Long seckillId){
        SeckillGoodVO seckillGoodVO = seckillGoodService.queryByIdCache(seckillId);
        return Result.success(seckillGoodVO);
    }

    @RequestMapping("/initData")
    public String initData(){
        //把库存放到redis中
        //数据库中有两条数据 11 12
        List<SeckillGoodVO> query = seckillGoodService.query();
        for (SeckillGoodVO seckillGoodVO : query) {
            redisService.set(SeckillKeyPrefixUtil.SECKILL_STOCK_COUNT,  seckillGoodVO.getId()+"",seckillGoodVO.getStockCount() );

            redisService.hset(SeckillKeyPrefixUtil.SECKILL_HASH, "", seckillGoodVO.getId()+"", seckillGoodVO);
        }
        return "初始化成功";
    }
}
